package com.service.admin.member.controller;

import com.google.common.collect.Lists;
import com.service.admin.member.controller.rep.MemberAuditPgeRep;
import com.service.admin.member.controller.rep.MemberPgeRep;
import com.service.admin.member.controller.rep.MemberRep;
import com.service.admin.member.controller.req.*;
import com.service.admin.member.enums.MemberEnums;
import com.service.admin.member.service.MemberService;
import com.service.admin.member.service.MemberVerifiedService;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.MultiResponse;
import com.service.core.rest.response.OneResponse;
import com.service.core.rest.response.PageResponse;
import com.service.dao.enums.CoinsEnum;
import com.service.utility.common.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年08月11日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Api(tags = "会员信息")
@RestController
@RequestMapping("member")
public class MemberController extends BaseController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberVerifiedService memberVerifiedService;

    @ApiOperation("分页")
    @PostMapping("page")
    public PageResponse<MemberPgeRep> page(@ApiParam @RequestBody MemberPageReq userReq) {
        return successQueryPageResponse(memberService.pageMember(userReq));
    }

    @ApiOperation("list")
    @PostMapping("list")
    public MultiResponse<MemberPgeRep> list(@RequestBody MemberPageReq userReq) {
        userReq.setMemberStatus(MemberEnums.Status.ACTIVE);
        return successQueryMultiResponse(memberService.listMember(userReq));
    }

    @ApiOperation("批量删除")
    @PostMapping("batch-delete")
    public BaseResponse batchDeleteMember(@ApiParam @RequestBody List<Integer> ids) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", ids, uuid);
        memberService.deleteMember(ids);
        log.info("请求结束： UUID:{}", uuid);
        return successDeleteBaseResponse();
    }

    @ApiOperation("单个删除删除")
    @PostMapping("delete/{memberId}")
    public BaseResponse deleteMember(@ApiParam @PathVariable("memberId") Integer memberId) {
        memberService.deleteMember(Lists.newArrayList(memberId));
        return successDeleteBaseResponse();
    }

    @ApiOperation("批量无效")
    @PostMapping("batch-invalid")
    public BaseResponse batchInvalidMember(@ApiParam @PathVariable("memberId") Integer memberId) {
        memberService.invalidMember(Lists.newArrayList(memberId));
        return successDeleteBaseResponse();
    }

    @ApiOperation("单个无效")
    @PostMapping("invalid/{memberId}")
    public BaseResponse invalidMember(@ApiParam @PathVariable("memberId") Integer memberId) {
        memberService.invalidMember(Lists.newArrayList(memberId));
        return successDeleteBaseResponse();
    }

    @ApiOperation("批量冻结")
    @PostMapping("batch-lock")
    public BaseResponse batchLockMember(@ApiParam @PathVariable("memberId") Integer memberId) {
        memberService.lockMember(Lists.newArrayList(memberId));
        return successDeleteBaseResponse();
    }

    @ApiOperation("单个冻结")
    @PostMapping("lock/{memberId}")
    public BaseResponse lockMember(@ApiParam @PathVariable("memberId") Integer memberId) {
        memberService.lockMember(Lists.newArrayList(memberId));
        return successDeleteBaseResponse();
    }


    @ApiOperation("重置登录密码")
    @PostMapping("reset-pwd/login")
    public BaseResponse resetLoginPwd(@ApiParam @RequestBody @Validated ResetPwdReq resetPwdReq) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", resetPwdReq, uuid);
        resetPwdReq.setType(0);
        memberService.resetPwd(resetPwdReq);
        log.info("请求结束： UUID:{}", uuid);
        return successUpdateBaseResponse();
    }

    @ApiOperation("重置支付密码")
    @PostMapping("reset-pwd/pay")
    public BaseResponse resetPayPwd(@ApiParam @RequestBody @Validated ResetPwdReq resetPwdReq) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", resetPwdReq, uuid);
        resetPwdReq.setType(1);
        memberService.resetPwd(resetPwdReq);
        log.info("请求结束： UUID:{}", uuid);
        return successUpdateBaseResponse();
    }

    @ApiOperation("会员详情")
    @PostMapping("detail/{id}")
    public OneResponse<MemberRep> getMemberDetail(@ApiParam @PathVariable("id") Integer id) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", id, uuid);
        MemberRep result = memberService.detail(id);
        log.info("请求结束： UUID:{}", uuid);
        return successQueryOneResponse(result);
    }

    @ApiOperation("修改会员等级")
    @PostMapping("modify-member-grade")
    public BaseResponse modifyMemberGrade(@RequestBody @Validated ModifyMemberGradeReq req) {
        memberService.updateMemberGrade(req);
        return successUpdateBaseResponse();
    }

    @ApiOperation("修改会员星级")
    @PostMapping("modify-member-star-grade")
    public BaseResponse modifyMemberStarGrade(@RequestBody @Validated ModifyMemberStarGradeReq req) {
        memberService.updateMemberStarGrade(req);
        return successUpdateBaseResponse();
    }

    @ApiOperation("给会员添加新货币资产")
    @PostMapping("add-new-coins/{coins}")
    public BaseResponse addNewCoins2MemberAssert(@PathVariable("coins") CoinsEnum coinsEnum) {
        memberService.addNewCoin2MemberAssert(coinsEnum);
        return successUpdateBaseResponse();
    }

    @ApiOperation("审核申请列表")
    @PostMapping("audit-list")
    public PageResponse<MemberPgeRep> page(@ApiParam @RequestBody AuditQueryPageReq auditQueryPageReq) {
        return successQueryPageResponse(memberVerifiedService.pageAudit(auditQueryPageReq));
    }

    @ApiOperation("审核用户实名认证")
    @PostMapping("audit-verify-status")
    public BaseResponse auditMember(@RequestBody @Validated AuditMemberVerifyReq req) {
        memberVerifiedService.auditVerifyStatus(req);
        return successUpdateBaseResponse();
    }

    @ApiOperation("会员下级")
    @PostMapping("children/{id}")
    public OneResponse getMemberChildren(@ApiParam @PathVariable("id") Integer id) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", id, uuid);
        return successQueryOneResponse(memberService.selectByPid(id));
    }

    @ApiOperation("修改冻结状态")
    @PostMapping("updateStoreFreezeStatus")
    public BaseResponse updateStoreFreezeStatus(@RequestBody UpdateStoreFreezeStatusReq req) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", req, uuid);

        memberService.updateStoreFreezeStatus(req);

        return successUpdateBaseResponse();
    }
}
